package com.hw.one.dam2.dao;

import com.hw.one.dam2.common.DamConstant;
import com.hw.one.dam2.param.PollutionParam;
import com.hw.one.dam2.param.SiteAirParam;
import com.hw.one.dam2.util.CalendarUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * Copyright 2017 汉威.智慧环保事业部.
 * Created by 李亚坤 on 2017/07/10 11:09.
 */
@Repository(value = "pollutionDao")
public class PollutionDao implements IPollutionDao {

    @Autowired
    private MongoTemplate mongoTemplateStandardSiteData;

    @Override
    public List<Map> findPollutionMinuteDataByTimeAndSNCodeList(PollutionParam pollutionParam) {
        // 查询条件 增加时间查询条件
        Criteria criteria = Criteria.where("SNCODE").in(pollutionParam.getSNCodeList()).
                and("MONITORTIME").gte(pollutionParam.getStartTime()).lte(pollutionParam.getEndTime());

        // 创建查询对象,以时间倒序排列
        Query query = new Query(criteria)
                .with(new Sort(Sort.Direction.DESC, "MONITORTIME"));
        List<Map> newDtaList = mongoTemplateStandardSiteData.find(query, Map.class, "T_DATA_SC_COLLECT_MIN5");
        return newDtaList;
    }

    @Override
    public List<Map> findPollutionHourDataByTimeAndSNCodeList(PollutionParam pollutionParam) {
        // 查询条件 增加时间查询条件
        Criteria criteria = Criteria.where("SNCODE").in(pollutionParam.getSNCodeList()).
                and("MONITORTIME").gte(pollutionParam.getStartTime()).lte(pollutionParam.getEndTime());

        // 创建查询对象,以时间倒序排列
        Query query = new Query(criteria)
                .with(new Sort(Sort.Direction.DESC, "MONITORTIME"));
        List<Map> newDtaList = mongoTemplateStandardSiteData.find(query, Map.class, "T_DATA_SC_COLLECT_HOUR");
        return newDtaList;
    }

    @Override
    public List<Map> findPollutionHourNewDataBySNCodeList(List<String> snCodeList) {
        // 获取最新记录
        Map newData = mongoTemplateStandardSiteData.findOne(
                new Query(Criteria.where("SNCODE").in(snCodeList)).with(new Sort(Sort.Direction.DESC, "MONITORTIME")), Map.class,
                "T_DATA_SC_COLLECT_HOUR");
        if(null == newData || newData.isEmpty()){
            return null;
        }
        // 得到最新时间
        Object timeObj = newData.get("MONITORTIME");
        // 查询条件 增加时间查询条件
        Criteria criteria = Criteria.where("SNCODE").in(snCodeList).and("MONITORTIME").is(timeObj);

        // 创建查询对象
        Query query = new Query(criteria)
                .with(new Sort(Sort.Direction.DESC, "MONITORTIME"));
        List<Map> newDtaList = mongoTemplateStandardSiteData.find(query, Map.class, "T_DATA_SC_COLLECT_HOUR");
        return newDtaList;
    }
}
